﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFTray
{
	public partial class WndConfig : Form
	{
		UiUtility.SwitchButtonWindows switchBtnWnds = new UiUtility.SwitchButtonWindows();
		public WndConfig()
		{
			InitializeComponent();
			TopLevel = false;
			FormClosing += (s, e) =>
			{
				try
				{
					switchBtnWnds.Close();
				}
				catch (Exception ex)
				{
					LogUtility.LOG.Error("WndConfig.FormClosing", ex);
				}
			};

			VisibleChanged += (s, e) =>
			  {
				  if (Visible == false)
				  {
					  foreach (var fm in forms)
					  {
						  fm.ParentHide();
					  }
				  }
				  else
				  {
					  switchBtnWnds.ShowWnd(switchBtnWnds.Index_Wnd);
				  }
			  };
		}

		List<WndConfigVision> forms = new List<WndConfigVision>();
		List<string> names = new List<string>();
		List<int> authes = new List<int>();

		WndConfigServer wndServer = new WndConfigServer();
		WndConfigRecipe wndRecipe = new WndConfigRecipe();
		internal void Init()
		{
			forms = new List<WndConfigVision>();
			names = new List<string>();
			authes = new List<int>();

			foreach (var f in GlobalParams.PC.Feeders)
			{
				var wnd = new WndConfigVision() { Feed = f };
				wnd.Init();
				f.wndCfg = wnd;
				forms.Add(wnd);
				names.Add(f.GetCfgFeed().Name);
				authes.Add(f.GetCfgFeed().AuthUI);
			}

			List<Form> tfes = new List<Form>();
			tfes.AddRange(forms);

			tfes.Add(wndRecipe);
			names.Add("配方");
			authes.Add(20);

			tfes.Add(wndServer);
			names.Add("通信");
			authes.Add(30);

			flowLayoutPanel1.BackColor = Color.MediumAquamarine;
			switchBtnWnds.ButtonSize = new Size(114, 45);
			switchBtnWnds.ButtonFontSize = 10.0F;
			switchBtnWnds.ButtonBold = true;
			switchBtnWnds.Add(tfes.ToArray(), names.ToArray(), authes.ToArray(), tableLayoutPanel1, 0, 0, flowLayoutPanel1);
			switchBtnWnds.UpdateAuth(GlobalParams.Auth);
			switchBtnWnds.ShowWnd(0);

		}

		internal void OnUiTimer()
		{
			if (!Visible) return;
			try
			{
				foreach (var f in forms)
				{
					f.OnUiTimer();
				}
				wndServer.OnUiTimer();
				wndRecipe.OnUiTimer();
			}
			catch (Exception ex)
			{
				LogUtility.LOG.Error("WndManual.OnUiTimer", ex);
			}
		}

		internal void UpdateAuth()
		{
			switchBtnWnds.UpdateAuth(GlobalParams.Auth);
		}

		internal void NotifyAxes(int index_feeder, float x, float y)
		{
			try
			{
				forms[index_feeder].NotifyAxesCoord(x, y);
			}
			catch(Exception ex)
			{
				LogUtility.LOG.Error("NotifyAxes", ex);
			}
		}
	}
}
